package com.eastedu.superise.handler.assignmen;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.eastedu.client.assignment.ResourceAssignmentClient;
import com.eastedu.repository.assignment.mapper.AssignmentMapper;
import com.eastedu.repository.assignment.po.AssignmentPO;
import com.eastedu.superise.handler.AbstractHistoricalDataHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Objects;

/**
 * 更新应用者
 * @author zhangzhishuai
 */
@Slf4j
@Service
@DS("assignment")
@RequiredArgsConstructor
public class AssignmentTextOrderHistoricalDataHandler extends AbstractHistoricalDataHandler<AssignmentPO> {

    public static final Integer TEXT_ORDER_TYPE_SCANTRON = 1;
    public static final String TEXT_ORDER_TYPE_SCANTRON_NAME = "SCANTRON";
    private final AssignmentMapper assignmentMapper;
    private final ResourceAssignmentClient resourceAssignmentClient;


    @Override
    protected String getSql() {
        return "SELECT " +
                    "* " +
                "FROM " +
                    "t_assignment_resource " +
                "WHERE " +
                    // "audit_state=2 " +
                    // "and (question_number >1 or question_number is null) " +
                    // "and (question_groups is not null and JSON_LENGTH(question_groups) > 0) " +
                    // "and classify IN (1, 4) " +
                    // "and `subject` in ('YY', 'YW') " +
                    // "and ( " +
                    // "  owner_name like '%作业' " +
                    // " OR " +
                    // "  owner_name like '%题单' " +
                    // " OR " +
                    // "  owner_name like '%试卷' " +
                    // " OR " +
                    // "  owner_organization_name like '闻道资源校' " +
                    // ")"
                " id=1144211152423047168"
                ;
    }

    @Override
    public void callback(AssignmentPO po) throws SQLException {
        if(Objects.equals(po.getTextOrderType(), TEXT_ORDER_TYPE_SCANTRON)){
            return;
        }
        resourceAssignmentClient.changeTextOrder(po.getId(), TEXT_ORDER_TYPE_SCANTRON_NAME);
        assignmentMapper.updateTextOrderType(TEXT_ORDER_TYPE_SCANTRON, po.getId());
    }
}
